package com.alibaba.controller;


import com.alibaba.bean.Token;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@RestController
@RestController
@RequestMapping("/oss")
public class OssController {

    @Autowired
    private Token token;

    @RequestMapping("/getOssToken")
    public void getOssToken(HttpServletResponse response) throws IOException {

        Token t = new Token();
        t.setEndPoint("sts.cn-beijing.aliyuncs.com");
        t.setAccessKeyId("LTAI5tEDxwoLG7Xokjy5YHaS");
        t.setAccessKeySecret("EPN34R207WGcXdxbQ3NZapM6kBVAxM");
        t.setRoleArn("acs:ram::1575021413247443:role/ramosstest");
        t.setRoleSessionName("GetToken");
        t.setPolicy("{\n" +
                "    \"Version\": \"1\", \n" +
                "    \"Statement\": [\n" +
                "        {\n" +
                "            \"Action\": [\n" +
                "                \"oss:PutObject\"\n" +
                "            ], \n" +
                "            \"Resource\": [\n" +
                "                \"acs:oss:*:*:mm-l-bucket/*\" \n" +
                "            ], \n" +
                "            \"Effect\": \"Allow\"\n" +
                "        }\n" +
                "    ]\n" +
                "}");
        t.setRegionId("oss-cn-beijing");

        DefaultProfile.addEndpoint(t.getRegionId(), "Sts", t.getEndPoint());
        IClientProfile profile = DefaultProfile.getProfile(t.getRegionId(), t.getAccessKeyId(),
                t.getAccessKeySecret());
        DefaultAcsClient client = new DefaultAcsClient(profile);
        final AssumeRoleRequest request = new AssumeRoleRequest();
        request.setSysMethod(MethodType.POST);
        request.setRoleArn(t.getRoleArn());
        request.setRoleSessionName(t.getRoleSessionName());
        request.setPolicy(t.getPolicy());
        request.setDurationSeconds(43200L);
        try {
            final AssumeRoleResponse res = client.getAcsResponse(request);
            t.setExpiration(res.getCredentials().getExpiration());
            t.setSecurityToken(res.getCredentials().getSecurityToken());
            t.setRequestId(res.getRequestId());
            t.setAccessKeyId(res.getCredentials().getAccessKeyId());
            t.setAccessKeySecret(res.getCredentials().getAccessKeySecret());

            JSONObject jsonObject= (JSONObject) JSONObject.toJSON(t);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(jsonObject.toJSONString());
        } catch (ClientException e) {
            System.out.println("Failed");
        }

    }

}
